package com.matt.controller;

import com.alibaba.fastjson.JSON;
import com.aliyun.oss.HttpMethod;
import com.dotflat.fin.utils.excelUtil.ExcelUtil;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.controller.po.ClienteleSortCountPO;
import com.matt.domain.Clientele;
import com.matt.domain.Contract;
import com.matt.domain.Leads;
import com.matt.domain.WorkDate;
import com.matt.ext.aliyun.OSSProcessService;
import com.matt.foundation.model.StatusResponse;
import com.matt.foundation.utils.DateUtil;
import com.matt.foundation.vo.ClienteleVo;
import com.matt.foundation.vo.LeadsVo;
import com.matt.service.ClienteleService;
import com.matt.service.CrmCountService;
import com.matt.remoteCall.SsoCallService;
import com.matt.service.LeadsService;
import com.matt.service.TeachCallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.*;

import static com.matt.foundation.constant.CommonConstant.TICKET;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/8/3
 * *******************************
 */
@RestController
@RequestMapping("/other_system")
@Api(tags = "其他系统 - 统计使用")
public class OtherSystemController {

    @Autowired
    private LeadsService leadsService;

    @Autowired
    private ClienteleService clienteleService;

    @Autowired
    private CrmCountService crmCountService;

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private TeachCallService teachCallService;

    @Autowired
    private OSSProcessService ossProcessService;

    @Value("${aliyun.oss.fileServerPrefix}")
    private String IMG_SERVICE_PREFIX;

    @RequestMapping(value = "/create_clientele")
    @ResponseBody
    @ApiOperation(value = "创建客户", httpMethod = "POST")
    public StatusResponse addClientele(@RequestHeader String ticket, Leads leads) {
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);

        //todo 需要修改 多个联系人
        leads.setAreaId(staff.getAreaId());
        leads.setStaffId(staff.getId());
        leads.setStaffName(staff.getRealName());
        leads.setLastFollowTime(new Date());
        leads.setLastModified(new Date());

        leads.setIsComplete("是");
        leads.setChangeStatus(1);
        leads.setChangeStatusVal("已转");

        //先创建Leads
        LeadsVo savedLeads = leadsService.save(leads, ticket);
        leads.setId(savedLeads.getId());

        //创建合同
        return StatusResponse.success(clienteleService.addByLeads(leads));
    }


    @RequestMapping(value = "/create_clienteleV2")
    @ResponseBody
    @ApiOperation(value = "创建客户", httpMethod = "POST")
    public StatusResponse addClientele(@RequestHeader String ticket,String areaId, Leads leads) {
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);

        leads.setAreaId(areaId);
        leads.setStaffId(staff.getId());
        leads.setStaffName(staff.getRealName());
        leads.setLastFollowTime(new Date());
        leads.setLastModified(new Date());

        leads.setIsComplete("是");
        leads.setChangeStatus(1);
        leads.setChangeStatusVal("已转");

        //先创建Leads
        LeadsVo savedLeads = leadsService.save(leads,areaId, ticket);
        leads.setId(savedLeads.getId());
        //创建合同
        return StatusResponse.success(clienteleService.addByLeads(leads));
    }


    @RequestMapping(value = "/create_leads")
    @ResponseBody
    @ApiOperation(value = "创建线索", httpMethod = "POST")
    public StatusResponse addLeads(Leads leads, String source) {

        leads.setIsComplete("否");
        leads.setChangeStatus(0);
        leads.setChangeStatusVal("未转");

        //先创建Leads
        Leads savedLeads = leadsService.saveToNoneOne(leads, source);

        //创建合同
        return StatusResponse.success(savedLeads);
    }


    //跟进客户数  [顾问个人每月跟进线索或客户的客户总数（同一客户只计算一次）（CRM数据）]
    @RequestMapping(value = "/follow_clientele_num")
    @ResponseBody
    @ApiOperation(value = "跟进客户数  [顾问个人每月跟进线索或客户的客户总数（同一客户只计算一次）（CRM数据）", httpMethod = "POST")
    public StatusResponse followClientleNum(String staffId, String date){
        Long count = crmCountService.followClienteleNum(staffId,date);
        return StatusResponse.success(count);
    }

    //本月签约所有合同数 [顾问个人每月签约的所有合同总数（包含正式合同与体验合同）（CRM数据）]
    @RequestMapping(value = "/sign_all_contract_num")
    @ApiOperation(value = "本月签约所有合同数 [顾问个人每月签约的所有合同总数（包含正式合同与体验合同）（CRM数据）]", httpMethod = "POST")
    public StatusResponse signAllContractNum(String staffId,  String date){
        //StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        Long count = crmCountService.signAllContractNum(staffId,date);
        return StatusResponse.success(count);
    }

    //签约正式合同数 [顾问个人每月签约正式合同总数（少于一个月的合同不计算入内）（CRM数据）]
    @RequestMapping(value = "/sign_formal_contract_num")
    @ApiOperation(value = "签约正式合同数 [顾问个人每月签约正式合同总数（少于一个月的合同不计算入内）（CRM数据）]", httpMethod = "POST")
    public StatusResponse signFormalContractNum(String staffId, String date){
        //StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        Long count = crmCountService.signFormalContractNum(staffId,date);
        return StatusResponse.success(count);
    }

    //新生数 [顾问个人每月签约的正式合同生效数（CRM数据）]
    @RequestMapping(value = "/sign_effective_contract_num")
    @ApiOperation(value = "新生数 [顾问个人每月签约的正式合同生效数（CRM数据）]", httpMethod = "POST")
    public StatusResponse signEffectiveContractNum(String staffId, String date){
        //StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        Long count = crmCountService.signEffectiveContractNum(staffId,date);
        return StatusResponse.success(count);
    }

    //面谈客户数 [顾问个人每月跟进状态为已到访的客户数（CRM数据）]
    @RequestMapping(value = "/view_clientele_num")
    @ApiOperation(value = "面谈客户数 [顾问个人每月跟进状态为已到访的客户数（CRM数据）]", httpMethod = "POST")
    public StatusResponse viewClienteleNum(String staffId, String date){
        //StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        Long count = crmCountService.viewClienteleNum(staffId,date);
        return StatusResponse.success(count);
    }

    //新客户数 [顾问每个月新增的客户总数（线索不算在内）（CRM数据）]
    @RequestMapping(value = "/increase_clientele_num")
    @ApiOperation(value = "新客户数 [顾问每个月新增的客户总数（线索不算在内）（CRM数据）]", httpMethod = "POST")
    public StatusResponse increaseClienteleNum(String staffId, String date){
        //StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        Long count = crmCountService.increaseClienteleNum(staffId,date);
        return StatusResponse.success(count);
    }

    //访客数 [前台每个月新增的“到店来访”线索数（CRM数据）]
    @RequestMapping(value = "/arrive_leads_num")
    @ApiOperation(value = "访客数 [前台每个月新增的“到店来访”线索数（CRM数据）]", httpMethod = "POST")
    public StatusResponse arriveLeadsNum(String staffId, String date){
        //StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        Long count = crmCountService.arriveLeadsNum(staffId,date);
        return StatusResponse.success(count);
    }

    //签约正式合同数（园区）[园区每月签约正式合同总数（少于一个月的合同不计算入内）（CRM数据）]
    @RequestMapping(value = "/area_contract_num")
    @ApiOperation(value = "签约正式合同数（园区）[园区每月签约正式合同总数（少于一个月的合同不计算入内）（CRM数据）]", httpMethod = "POST")
    public StatusResponse areaContractNum(String areaId, String date){
        //StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        Long count = crmCountService.areaContractNum(areaId,date);
        return StatusResponse.success(count);
    }

    //合同履行中的客户数 [顾问个人本月合同正在履行中的合同数（CRM数据）]
    @RequestMapping(value = "/proceed_contract_num")
    @ApiOperation(value = "合同履行中的客户数 [顾问个人本月合同正在履行中的合同数（CRM数据）]", httpMethod = "POST")
    public StatusResponse proceedContractNum(String staffId, String date){
        //StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        Long count = crmCountService.proceedContractNum(staffId,date);
        return StatusResponse.success(count);
    }

    //客户宝宝入园数(顾问) [顾问所签约的客户宝宝每日的入园总数 (CRM数据)+（教务数据）]
    @RequestMapping(value = "/adviser_child_check_in_count")
    @ApiOperation(value = "客户宝宝入园数(顾问) [顾问所签约的客户宝宝每日的入园总数 (CRM数据)+（教务数据）]", httpMethod = "POST")
    public StatusResponse adviserChildCheckInCount(String staffId, String date){
        //StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        Long count = crmCountService.adviserChildCheckInCount(staffId,date);
        return StatusResponse.success(count);
    }

    //跟进数
    @RequestMapping(value = "/push_follow_count")
    @ApiOperation(value = "跟进数", httpMethod = "POST")
    public StatusResponse pushFollowNum(String staffId){
        //StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        return StatusResponse.success(crmCountService.pushFollowNum(staffId));
    }

    //顾问 周报数据
    @RequestMapping(value = "/get_adviser_week_data")
    @ApiOperation(value = "顾问 周报数据", httpMethod = "POST")
    public StatusResponse getAdviserWeekData(String staffId, String date){
        //StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        return StatusResponse.success(crmCountService.getAdviserWeekData(staffId, staffId, date));
    }

    //园区 周报数据
    @RequestMapping(value = "/get_area_week_data")
    @ApiOperation(value = "园区 周报数据", httpMethod = "POST")
    public StatusResponse getAreaWeekData(String staffId, String date){
        //StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        return StatusResponse.success(crmCountService.getAreaWeekData(staffId, date));
    }

    //获取最新的合同信息
    @RequestMapping(value = "/get_contract")
    @ApiOperation(value = "园区 指定一条学生数据", httpMethod = "POST")
    public StatusResponse findByClienteleIdDesc(String areaId, String clienteleId){
        return StatusResponse.success(crmCountService.findByClienteleIdDesc(areaId, clienteleId));
    }

    @RequestMapping(value = "/update_contract")
    @ApiOperation(value = "指定学生时间内数据", httpMethod = "POST")

    public StatusResponse updateContract(String areaId,String id,String realName, String clienteleId, String date,  Integer day, Integer type){
        List<WorkDate> workDateList = teachCallService.getWorkDateList(null,null);
        StaffDTO staffDTO = new StaffDTO();
        staffDTO.setAreaId(areaId);
        staffDTO.setId(id);
        staffDTO.setRealName(realName);
        return StatusResponse.success(crmCountService.updateContract(staffDTO, clienteleId, date+" 00:00:00", workDateList, day,type));
    }

    //指定学生时间内数据
    @RequestMapping(value = "/get_clienteleId_contract_list")
    @ApiOperation(value = "指定学生时间内数据", httpMethod = "POST")

    public StatusResponse get_clienteleId_contract_list(String areaId, String clienteleId, String date){
        return StatusResponse.success(crmCountService.findByClienteleIdByList(areaId, clienteleId, date));
    }

    @RequestMapping(value = "/detail")
    @ApiOperation(value = "客户详情", httpMethod = "GET", response = ClienteleVo.class)
    public StatusResponse detail(String clientelesId) {
        return StatusResponse.success(clienteleService.findById(clientelesId));
    }

    @RequestMapping(value = "/attendance_count")
    @ApiOperation(value = "应出勤天数", httpMethod = "GET", response = Integer.class)
    public StatusResponse attendanceCount(String areaId,String clientelesId, String date) {
//        String date1= null;
//        if (Objects.nonNull(date) && !StringUtils.equals("null",date)){
//            date1 =  date+" 00:00:00";
//        }
//        List<WorkDate> workDateList = teachCallService.getWorkDateList(date1,DateUtil.getMonthLast(date));
//        return StatusResponse.success(crmCountService.attendanceCount(areaId,clientelesId,date,workDateList));
        return StatusResponse.success(crmCountService.attendanceCount(areaId,clientelesId,date));
    }

    @RequestMapping(value = "/clientelesIdByContractList")
    @ApiOperation(value = "客户IDS获取合同集合", httpMethod = "POST", response = Contract.class)
    public StatusResponse clientelesIdByContractList(String clientelesIds) {
        List<Contract>  list = crmCountService.findByClienteleIds(clientelesIds);
        return StatusResponse.success(list);
    }

}
